package com.myapplication.zhou.alldemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.myapplication.callback.ChangePassowrdCallback;
import com.myapplication.interfaces.ChangePasswordConnect;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class ChangePasswordActivity extends AppCompatActivity implements View.OnClickListener, ChangePassowrdCallback {

    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.textView_title)
    private TextView textView_title;
    @ViewInject(R.id.button_login)
    private Button button_login;
    @ViewInject(R.id.edit_enter_password_first)
    private EditText edit_enter_password_first;
    @ViewInject(R.id.edit_enter_password_second)
    private EditText edit_enter_password_second;
    public String password1, password2;
    @ViewInject(R.id.textView_title)
    private TextView TextView_title;

    private ChangePasswordConnect changePasswordConnect;
    private String auth_code;
    private String phonenumber;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        //通过ViewInject注入布局
        x.view().inject(this);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Bundle bundle = getIntent().getExtras();
        auth_code = bundle.getString("auth_code");
        phonenumber = bundle.getString("phonenumber");
        changePasswordConnect = new ChangePasswordConnect();

        textView_title.setText("修改密码");
        button_login.setOnClickListener(this);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
               case R.id.button_login:
                if (isMatch()) {
                    changePasswordConnect.changePassword(phonenumber,
                            auth_code,
                            password1,
                            "android",
                            this);

                }

                break;

        }
    }


    public boolean checknullable() {
        boolean b = false;
        password1 = edit_enter_password_first.getText().toString().trim();
        password2 = edit_enter_password_second.getText().toString().trim();
        if (password1.length() == 0 && password2.length() == 0) {
            b = false;
            Snackbar.make(edit_enter_password_first, "输入框不能为空！", Snackbar.LENGTH_SHORT).show();
        } else
            b = true;

        return b;
    }

    public boolean isMatch() {
        boolean b = false;
        if (checknullable()) {
            if (password2.equals(password1)) {
                b = true;
            } else {
                b = false;
                Snackbar.make(edit_enter_password_second,"密码不一致，请重新输入!",Snackbar.LENGTH_SHORT).show();
            }
        } else
            b = false;
        return b;
    }

    @Override
    public void OnChangePasswordSuccess(String changePasswordPasswordSuccessResult) {
        Log.i("changePasswordR...",changePasswordPasswordSuccessResult);
        if(!changePasswordPasswordSuccessResult.contains("data")){

            Snackbar.make(edit_enter_password_second,"服务器正在维修，请稍后重试！",Snackbar.LENGTH_SHORT).show();
        }else{

            try {
                JSONObject jsonObject = new JSONObject(changePasswordPasswordSuccessResult);
                int datas = jsonObject.optInt("datas");
                if(datas==1){
                    Intent intent = new Intent();
                    intent.setClass(ChangePasswordActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public void OnChangePassowrdFail(String changePasswordPasswordSuccessFail) {
        Snackbar.make(edit_enter_password_second,changePasswordPasswordSuccessFail,Snackbar.LENGTH_SHORT).show();
    }
}
